Skip to main content

Class Diagram

It describe the structure of a system by showing its classes, attributes, methods, and the relationships between them.

Components

  • Class: The basic building block of a class diagram, representing a blueprint of an object in the system. A class is typically depicted as a rectangle divided into three sections:
    • Class Name: The top section contains the class name, usually written in bold.
    • Attributes: The middle section lists the data members or properties of the class.
    • Methods: The bottom section lists the functions or operations that the class can perform.
  • Attributes: The data stored within a class (also known as fields or properties). Attributes have types (e.g., String, Integer) and can have visibility modifiers:
    • Public (+): Can be accessed from outside the class.
    • Private (-): Can only be accessed within the class.
    • Protected (#): Can be accessed within the class and its subclasses.
  • Methods: The operations or functions a class can perform. Like attributes, methods can also have visibility modifiers. Methods define the behavior of the class and typically manipulate its attributes.
  • Relationships between Classes:
    • Association: A general relationship between two classes. It can be unidirectional or bidirectional(A Member can borrow multiple Book, and a Book can be borrowed by multiple Member (over time)). It is represented by a line between the classes followed by an arrow that navigates the direction, and when the arrow is on both sides, it is then called a bidirectional association. We can specify the multiplicity of an association by adding the adornments on the line that will denote the association.
    • Multiplicity: Defines how many instances of one class can be associated with instances of another class (e.g., 1..*, 0..1).
    • Inheritance (Generalization): A "is-a" relationship where a subclass inherits from a parent class(Member and Librarian classes can inherit from a common base class called Person since both share attributes like name, contact information, etc).
    • Aggregation: A "has-a" relationship where one class is composed of another class but can exist independently.(Library "has" multiple Section.) It is represented by a straight line with an empty diamond at one end.
    • Composition: A stronger form of aggregation where the contained class cannot exist independently(The Library consists of Book. If the library is closed or removed, the books associated with it are also gone, so this is a strong composition relationship. The library is responsible for maintaining the collection of books). It is represented by a straight line with a black diamond at one end.
    • Dependency: A weaker relationship indicating that one class depends on another, but only temporarily(A Member depends on BorrowTransaction to borrow or return books. )
AssociationAggregationComposition
Association relationship is represented using an arrow.Aggregation relationship is represented by a straight line with an empty diamond at one end.The composition relationship is represented by a straight line with a black diamond at one end.
In UML, it can exist between two or more classes.It is a part of the association relationship.It is a part of the aggregation relationship.
It incorporates one-to-one, one-to-many, many-to-one, and many-to-many association between the classes.It exhibits a kind of weak relationship.It exhibits a strong type of relationship.
It can associate one more objects together.In an aggregation relationship, the associated objects exist independently within the scope of the system.In a composition relationship, the associated objects cannot exist independently within the scope of the system.
In this, objects are linked together.In this, the linked objects are independent of each other.Here the linked objects are dependent on each other.
It may or may not affect the other associated element if one element is deleted.Deleting one element in the aggregation relationship does not affect other associated elements.It affects the other element if one of its associated element is deleted.
Example: A tutor can associate with multiple students, or one student can associate with multiple teachers.Example: A car needs a wheel for its proper functioning, but it may not require the same wheel. It may function with another wheel as well.Example: If a file is placed in a folder and that folder is deleted, the file residing inside that folder will also get deleted at the time of folder deletion.

Example

Example:

+-----------------+       +------------------+
| Library | | LibraryMember |
+-----------------+ +------------------+
| name | | name |
| address | | membershipId |
+-----------------+ +------------------+
| addBook() | | borrowBook() |
| removeBook() | | returnBook() |
+-----------------+ +------------------+
| |
+-------------------------+
Association (Library contains LibraryMembers)

Example:

+------------------+    1    +------------------+
| Library |<--------| Section |
+------------------+ +------------------+
| - name: String | | - sectionName: String |
+------------------+ +------------------+
| + addBook() | | + listBooks() |
| + removeBook() | +------------------+
+------------------+
| *
|
| *
+------------------+ * +------------------+
| Book |<----------------| BorrowTransaction |
+------------------+ +------------------+
| - title: String | | - transactionId: String |
| - author: String | | - issueDate: Date |
| - ISBN: String | | - returnDate: Date |
+------------------+ +------------------+
| + getDetails() | | + issueBook() |
+------------------+ | + returnBook() |
+------------------+
|
| 1..*
|
+------------------+ * +------------------+
| Person |<---------------------| Member |
+------------------+ +------------------+
| - name: String | | - memberId: String|
| - address: String| +------------------+
+------------------+ | + borrowBook() |
| + getDetails() | | + returnBook() |
+------------------+ +------------------+
|
| 1
|
+------------------+
| Librarian |
+------------------+
| - employeeId: String|
+------------------+
| + manageBooks() |
+------------------+